//
//  TMGMakeMoneyViewController.m
//  TMGold
//
//  Created by Mac on 2018/3/17.
//  Copyright © 2018年 tangmu. All rights reserved.
//

#import "TMGMakeMoneyViewController.h"

@interface TMGMakeMoneyViewController ()
@property(nonatomic, strong) STLabel                     *cardNameLable;
@property(nonatomic, strong) UITextField                     *moneyTextFiled;
@end

@implementation TMGMakeMoneyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"提现";
    [self configSubView];
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    header.backgroundColor = [UIColor whiteColor];
    
    UIImageView * bannerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 10, UIScreenWidth - 30, 100)];
    bannerImageView.image = [UIImage imageNamed:@"bg_tixian"];
    bannerImageView.userInteractionEnabled = YES;
    [header addSubview:bannerImageView];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 10, 200, 44)
                                                     text:[NSString stringWithFormat:@"可提现金额%@",self.money]
                                                textColor:[UIColor whiteColor]
                                                     font:16
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    titleLable.centerX = UIScreenWidth / 2;
    titleLable.centerY = bannerImageView.height / 2;
    [bannerImageView addSubview:titleLable];
    
    
    STLabel * shoukuanLable = [[STLabel alloc] initWithFrame:CGRectMake(15, bannerImageView.bottom, 200, 44)
                                                     text:@"收款银行卡"
                                                textColor:FirstTextColor
                                                     font:15
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [header addSubview:shoukuanLable];
    
    NSString * bank = [STUserManger defult].loginUser.bank;
    self.cardNameLable = [[STLabel alloc] initWithFrame:CGRectMake(15, shoukuanLable.bottom, 200, 44)
                                                        text:bank
                                                   textColor:FirstTextColor
                                                        font:15
                                                 isSizetoFit:NO
                                               textAlignment:NSTextAlignmentLeft];
    self.cardNameLable.backgroundColor = [UIColor whiteColor];
    [header addSubview:self.cardNameLable];
    
    STLabel * tixianLable = [[STLabel alloc] initWithFrame:CGRectMake(15, self.cardNameLable.bottom, 200, 44)
                                                        text:@"提现金额"
                                                   textColor:FirstTextColor
                                                        font:15
                                                 isSizetoFit:NO
                                               textAlignment:NSTextAlignmentLeft];
    [header addSubview:tixianLable];
    
    UIView * whiteView = [[UIView alloc] initWithFrame:CGRectMake(0, tixianLable.bottom, UIScreenWidth, 44)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [header addSubview:whiteView];
    
    self.moneyTextFiled  = [[UITextField alloc] initWithFrame:CGRectMake(15, 0, UIScreenWidth, 44)];
    self.moneyTextFiled.keyboardType = UIKeyboardTypeNumberPad;
    self.moneyTextFiled.placeholder = @"请输入提现金额";
    [whiteView addSubview:self.moneyTextFiled];
    
    
    STLabel * tixianDesLable = [[STLabel alloc] initWithFrame:CGRectMake(15, whiteView.bottom, 200, 44)
                                                      text:@"提现说明"
                                                 textColor:TM_orangeColor
                                                      font:15
                                               isSizetoFit:NO
                                             textAlignment:NSTextAlignmentLeft];
    [header addSubview:tixianDesLable];
    
    NSString * name = @"提现手续费:5元每笔,每月第一笔免费\n提现限额:单笔提现不能小于5元,不大于5万元\n提现到账时间:1~3个工作日到账,节假日可能有所延迟";
    STLabel * desLable = [[STLabel alloc] initWithFrame:CGRectMake(20, tixianDesLable.bottom, UIScreenWidth - 40, 44)
                                                         text:name
                                                    textColor:FirstTextColor
                                                         font:15
                                                  isSizetoFit:NO
                                                textAlignment:NSTextAlignmentLeft];
    [NSString lableAutoAdjustheightWithLabel:desLable];
    [header addSubview:desLable];
    
    
    __weak typeof(self) weakSelf =  self;
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, desLable.bottom + 30, UIScreenWidth - 60 , 44)
                                                        title:@"确认提现"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:10
                                              backgroundColor:TM_orangeColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedLConfimButton];
    }];
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [header addSubview:confimButton];

    
    header.height = confimButton.bottom + 30;
    self.tableView.tableHeaderView = header;
}
#pragma mark --Action Method
- (void)onSelectedLConfimButton{
     NSString * bank = [STUserManger defult].loginUser.bank;
    if (!bank.length) {
        [SVProgressHUD showErrorWithStatus:@"请先绑定银行卡"];
        return;
        
    }
    if (!self.moneyTextFiled.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入金额"];
        return;
        
    }
    [self sendMakeMoneyRequest];
}
- (void)sendMakeMoneyRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    if (self.moneyTextFiled.text.length) {

        [dic setObject:self.moneyTextFiled.text forKey:@"money"];
        
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/personal/withdrawals"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"已经成功提交提现申请"];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 [self.tableView.mj_header endRefreshing];
                                             }];
}
@end
